package com.example.mycomposelesson.ui

import android.content.ContentValues.TAG
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mycomposelesson.util.LogUtil

@Preview(showBackground = true,
    widthDp = 1920,
    heightDp = 1024)
@Composable
fun previewTextPractise(){
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colors.background
    ){
        TextPractise()
    }
}

@Composable
fun TextPractise(){
    var openDialog = remember { mutableStateOf(false) }
    Column(verticalArrangement = Arrangement.Bottom,
    horizontalAlignment = Alignment.CenterHorizontally,
    modifier = Modifier.padding(bottom = 20.dp)) {
        val annotatedText = buildAnnotatedString {
            append("勾选即代表同意")
            pushStringAnnotation(
                tag = "tag",
                annotation = "一个用户协议啦啦啦，内容内容"
            )
            withStyle(
                style = SpanStyle(
                    color = Color(0xFF0E9FF2),
                    fontWeight = FontWeight.Bold
                )
            ) {
                append("用户协议")
            }
            pop()
        }
        SelectionContainer{
            ClickableText(
                text = annotatedText,
                style = MaterialTheme.typography.h4,
                onClick = { offset ->
                    annotatedText.getStringAnnotations(
                        tag = "tag", start = offset,
                        end = offset
                    ).firstOrNull()?.let { annotation ->
                        Log.d(TAG, "你已经点到 ${annotation.item} 啦")
                        openDialog.value=true
                    }
                })
        }

    }
    UserDialog(openDialog)
}

@Composable
fun UserDialog(openDialog: MutableState<Boolean>){
    if (openDialog.value) {
        AlertDialog(
            onDismissRequest = {
                // 当用户点击对话框以外的地方或者按下系统返回键将会执行的代码
                openDialog.value = false
            },
            title = {
                Text(
                    text = "用户协议",
                    fontWeight = FontWeight.W700,
                    style = MaterialTheme.typography.h6
                )
            },
            text = {
                Text(
                    text = "如果您看到这个界面，代表您已经同意我们所有的用户协议",
                    fontSize = 16.sp
                )
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        openDialog.value = false
                        LogUtil.logger.error { "对话框确认了" }
                    },
                ) {
                    Text(
                        "确认",
                        fontWeight = FontWeight.W700,
                        style = MaterialTheme.typography.button
                    )
                }
            },
            dismissButton = {
                TextButton(
                    onClick = {
                        openDialog.value = false
                        LogUtil.logger.error { "对话框取消了" }
                    }
                ) {
                    Text(
                        "取消",
                        fontWeight = FontWeight.W700,
                        style = MaterialTheme.typography.button
                    )
                }
            }
        )
    }
}